//
//  CaseManagerViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "CaseManagerViewController.h"
#import "LKProfileBtn.h"
#import "CaseTableViewCell.h"
#import "LKDeseaseTypeTBC.h"
#import "LKCaseModel.h"
//屏幕尺寸快捷获取
#define Ksize [UIScreen mainScreen].bounds.size
//定义一个统一间距
#define margin 15
//定义按钮高度
#define btnHeight 15
//定义数据存储路径
#define filePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"dataArray.plist"]
//定义重用标识符
static NSString* identifier = @"cell";
@interface CaseManagerViewController ()<UITableViewDataSource,UITableViewDelegate>

/**
 *  顶部个人信息视图
 */
@property(nonatomic,weak) UIView* profileView;

/**
 *  中间可滚动的病例视图
 */
@property(nonatomic,weak) UITableView* caseTableView;

/**
 *  底部添加病例视图
 */
@property(nonatomic,weak) UIView* addCaseView;

/**
 *  存储模型的数据数组
 */
@property(nonatomic,copy)NSMutableArray* dataArray;

@end

@implementation CaseManagerViewController
#pragma mark--声明周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    //创建UI
    [self setUpMyUI];
}
#pragma mark--创建UI
-(void)setUpMyUI
{
    //设置profileView的背景色,懒加载
    self.profileView.backgroundColor = [UIColor whiteColor];
    //创建底部的添加病例视图
    self.addCaseView.backgroundColor = [UIColor whiteColor];
    //创建caseTableView
    //注册cell
    [self.caseTableView registerClass:[CaseTableViewCell class] forCellReuseIdentifier:identifier];
}
#pragma mark--tableView数据源方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CaseTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    //cell被选中后不改变颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //cell赋值
    if (self.dataArray.count != 0) {
        cell.model = self.dataArray[indexPath.row];
    }
    return cell;
}

#pragma mark--tableView代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [SVProgressHUD showInfoWithStatus:@"正在开发中"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setMinimumDismissTimeInterval:1];
}

#pragma mark--按钮方法响应
-(void)didCLickAddCase
{
    LKDeseaseTypeTBC* deseaseTBC = [[LKDeseaseTypeTBC alloc] init];
    //为block赋值
    deseaseTBC.modelBlock = ^(LKCaseModel* model)
    {
        //增加数据
        [self.dataArray insertObject:model atIndex:0];
        //归档
        [NSKeyedArchiver archiveRootObject:self.dataArray toFile:filePath];
        //刷新界面
        [self.caseTableView reloadData];
    } ;
    [self.navigationController pushViewController:deseaseTBC animated:YES];
}
#pragma mark--UI懒加载
-(UIView *)profileView
{
    if(!_profileView)
    {
        //创建顶部UIView,命名为profileView
        UIView* profileView = [[UIView alloc] init];
        _profileView = profileView;
        [self.view addSubview:profileView];
        
        //设置profileView的约束
        [profileView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(0);
            make.height.equalTo(Ksize.height * 0.25);
        }];
        
        //为profileView添加子控件
        
        // 添加左侧的头像imageView, iconImageView,设置默认占位图片
        UIImageView* iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"name"]];
        
        //添加profileView中,先不设置约束,因为要基于底部label的约束才能设置
        [profileView addSubview:iconImageView];
        
        // 添加姓名label.nameLabel
        
        UILabel* nameLabel  = [[UILabel alloc] init];
        [profileView addSubview:nameLabel];
        
        //设置文本内容
        nameLabel.text = @"王二狗";
        nameLabel.textAlignment = NSTextAlignmentCenter;
        
        // nameLabel 添加约束
        [nameLabel  makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(profileView).offset(margin);
            make.bottom.equalTo(profileView).offset(-margin);
            make.width.equalTo(Ksize.width / 4);
        }];
        
        //iconImageView添加约束
        [iconImageView makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.equalTo(profileView).offset(margin);
            make.bottom.equalTo(nameLabel.top).offset(-margin);
            make.width.equalTo(Ksize.width / 4);
        }];
        
        //创建四个button
        //性别按钮
        LKProfileBtn* genderBtn = [[LKProfileBtn alloc] init];
        [profileView addSubview: genderBtn];
        
        //设置约束
        [genderBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(profileView).offset(2 *margin);
            make.left.equalTo(iconImageView.right).offset(margin);
            make.height.equalTo(btnHeight);
            make.right.equalTo(profileView).offset(-200);
        }];
        
        //设置内容
        [genderBtn setImage:[UIImage imageNamed:@"0"] forState:UIControlStateNormal];
        [genderBtn setTitle:@"男" forState:UIControlStateNormal];
        [genderBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        //创建年龄label
        UILabel* ageLabel = [[UILabel alloc] init];
        [profileView addSubview:ageLabel];
        //设置约束
        [ageLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(genderBtn);
            make.left.equalTo(genderBtn.right).offset(20);
        }];
        
        //设置内容
        ageLabel.text = @"28";
        ageLabel.textColor = [UIColor blackColor];
        
        //id按钮
        LKProfileBtn* idBtn = [[LKProfileBtn alloc] init];
        [profileView addSubview:idBtn];
        
        //设置约束
        [idBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(genderBtn.bottom).offset(10);
            make.left.equalTo(iconImageView.right).offset(margin);
            make.height.equalTo(btnHeight);
            make.right.equalTo(profileView).offset(-margin);
        }];
        
        //设置内容
        [idBtn setImage:[UIImage imageNamed:@"idCard"] forState:UIControlStateNormal];
        [idBtn setTitle:@"4534525324523453" forState:UIControlStateNormal];
        [idBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        //手机号码按钮
        LKProfileBtn* phoneNumBtn = [[LKProfileBtn alloc] init];
        [profileView addSubview:phoneNumBtn];
        
        //设置约束
        [phoneNumBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(idBtn.bottom).offset(10);
            make.left.equalTo(iconImageView.right).offset(margin);
            make.height.equalTo(btnHeight);
            make.right.equalTo(profileView).offset(-margin);
        }];
        
        //设置内容
        [phoneNumBtn setImage:[UIImage imageNamed:@"phone"] forState:UIControlStateNormal];
        [phoneNumBtn setTitle:@"1382345467" forState:UIControlStateNormal];
        [phoneNumBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        //定位按钮
        LKProfileBtn* locationBtn = [[LKProfileBtn alloc] init];
        [profileView addSubview:locationBtn];
        
        //设置约束
        [locationBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(phoneNumBtn.bottom).offset(10);
            make.left.equalTo(iconImageView.right).offset(margin);
            make.height.equalTo(btnHeight);
            make.right.equalTo(profileView).offset(-margin);
        }];
        
        //设置内容
        [locationBtn setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateNormal];
        [locationBtn setTitle:@"上海" forState:UIControlStateNormal];
        [locationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //添加银线
        UIView * lineView = [[UIView alloc] init];
        [profileView addSubview:lineView];
        lineView.backgroundColor = [UIColor lightGrayColor];
        [lineView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(profileView).offset(5);
            make.right.equalTo(profileView).offset(-5);
            make.height.equalTo(1);
            make.bottom.equalTo(profileView).offset(-5);
        }];
    }
    return _profileView;
}

-(UITableView *)caseTableView
{
    if (!_caseTableView) {
        //实例化tableView
        UITableView* caseTableView = [[UITableView alloc] init];
        _caseTableView = caseTableView;
        [self.view addSubview:caseTableView];
        //设置代理
        _caseTableView.delegate = self;
        _caseTableView.dataSource = self;
        //设置约束
        [caseTableView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.profileView.bottom);
            make.left.right.equalTo(self.view);
            make.height.equalTo(Ksize.height*0.65-64);
        }];
        //取消分隔线
        caseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //设置行高
        caseTableView.rowHeight = 120;
    }
    return _caseTableView;
}

-(UIView *)addCaseView
{
    if (!_caseTableView) {
        //实例化
        UIView* addCaseView = [[UIView alloc] init];
        _addCaseView = addCaseView;
        [self.view addSubview:addCaseView];
        //设置约束
        [addCaseView makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.height.equalTo(Ksize.height * 0.1);
        }];
        
        //添加一个button
        UIButton* addCaseBtn = [[UIButton alloc] init];
        [addCaseView addSubview:addCaseBtn];
        [addCaseBtn setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
        //设置约束
        [addCaseBtn makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(addCaseView).offset(7);
            make.bottom.equalTo(addCaseView).offset(-15);
            make.left.equalTo(addCaseView).offset(20);
            make.right.equalTo(addCaseView).offset(-20);
        }];
        //设置圆角
        addCaseBtn.layer.cornerRadius = 5;
        addCaseBtn.clipsToBounds = YES;
        //设置内容
        [addCaseBtn setTitle:@"添加病例" forState:UIControlStateNormal];
        //添加响应方法
        [addCaseBtn addTarget:self action:@selector(didCLickAddCase) forControlEvents:UIControlEventTouchUpInside];
    }
    return _caseTableView;
}

#pragma mark--数据懒加载
-(NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        //解档
        _dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        
    }
    if(_dataArray.count == 0 )
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
